package com.okaysoft.ofb

class TerminalClientUser implements Serializable{

    Terminal terminal
    ClientUser clientUser

    static TerminalClientUser create(Terminal terminal, ClientUser clientUser, boolean flush = false) {
        new TerminalClientUser(terminal: terminal, clientUser: clientUser).save(flush: flush, insert: true)
    }

    static TerminalClientUser get(long terminalId, long clientUserId) {
        find 'from TerminalClientUser where terminal.id=:terminalId and clientUser.id=:clientUserId',
                [terminalId: terminalId, clientUserId: clientUserId]
    }

    static boolean removeAll(Terminal terminal,ClientUser clientUser){
        def terminalClientUser = TerminalClientUser.findByClientUserAndTerminal(clientUser,terminal)
        if(!terminalClientUser){
            return false
        }
        terminalClientUser.delete(flush: true)
        true
    }

    static void removeAll(Terminal terminal){
        executeUpdate 'DELETE FROM TerminalClientUser WHERE terminal=:terminal', [terminal: terminal]
    }

    static void removeAll(ClientUser clientUser){
        executeUpdate 'DELETE FROM TerminalClientUser WHERE clientUser=:clientUser', [clientUser: clientUser]
    }

    static constraints = {
    }

    static mapping = {
        id composite: ['terminal', 'clientUser']
        version false
    }
}
